package com.bbx.taxi.client.Activity.Address;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Fragment.CarpoolingFragment;
import com.example.mylistview.swipemenu.MyListView;
import com.example.mylistview.swipemenu.MyListView.OnMenuItemClickListener;
import com.example.mylistview.swipemenu.MyListView.OnSwipeListener;
import com.example.mylistview.swipemenu.SwipeMenu;
import com.example.mylistview.swipemenu.SwipeMenuCreator;
import com.example.mylistview.swipemenu.SwipeMenuItem;

/**
 * 不同地点上下车
 */
public class DifferentAddressActivity extends BaseBBXActivity {
	public static final int RESULT_CODE = 0;
	@InjectView(R.id.MyListView)MyListView mMyListView;
	private RelativeLayout add;
	private List<Map<String, String>> list;
	private String setout_string;
	private String destination_string;
	private String telephone_string;
	private DifferentAddressAdapter mDifferentAddressAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.differentaddress);
		super.onCreate(savedInstanceState);
		setTitle("不同地点上下车");
		setout_string=getIntent().getStringExtra("setout");
		destination_string=getIntent().getStringExtra("destination");
		telephone_string=getIntent().getStringExtra("telephone");
		initswipe();
	}
	class DifferentAddressAdapter extends BaseAdapter{
		private List<Map<String, String>> list;
		private Activity main;
		public DifferentAddressAdapter(Activity context,List<Map<String, String>> list) {
			   this.list=list;
			   main=context;
			}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder viewholder;
			if(convertView==null){      
			LayoutInflater inflater=LayoutInflater.from(main);
			convertView=(View)inflater.inflate(R.layout.listitem_differentaddress,null);        
			viewholder=new ViewHolder();
			viewholder.id=(TextView)convertView.findViewById(R.id.id);	
			viewholder.setout=(TextView)convertView.findViewById(R.id.setout);	
			viewholder.destination=(TextView)convertView.findViewById(R.id.destination);	
			viewholder.telephone=(TextView)convertView.findViewById(R.id.telephone);	
			convertView.setTag(viewholder);	
			}
			else viewholder=(ViewHolder)convertView.getTag();
			viewholder.id.setText("地址"+(position+1));   
			viewholder.setout.setText(list.get(position).get("setout"));   
			viewholder.destination.setText(list.get(position).get("destination"));   
			viewholder.telephone.setText(list.get(position).get("tel"));
			return convertView;
		}
		public class ViewHolder{                   
			TextView id,setout,destination,telephone;
		}	
	}
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.add:
			startActivityForResult(new Intent(this,AddDifferentAddressActivity.class), RESULT_CODE);
			break;
		}
	}
	/**
	 * 初始化ListView的Item菜单
	 */
	public void initswipe() {

		// step 1. create a MenuCreator
		SwipeMenuCreator creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {
				// create "open" item
				SwipeMenuItem openItem = new SwipeMenuItem(
						getApplicationContext());
				// set item background
				openItem.setBackgroundColor(getResources()
						.getColor(R.color.red));
				// set item width
				openItem.setWidth(dp2px(85));
				// set item title
				openItem.setTitle("删除");
				// set item title fontsize
				openItem.setTitleSize(18);
				// set item title font color
				openItem.setTitleColor(Color.WHITE);
				// add to menu
				menu.addMenuItem(openItem);

				// // create "delete" item
				// SwipeMenuItem deleteItem = new SwipeMenuItem(
				// getApplicationContext());
				// // set item background
				// deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
				// 0x3F, 0x25)));
				// // set item width
				// deleteItem.setWidth(dp2px(90));
				// // set a icon
				// deleteItem.setIcon(R.drawable.ic_delete);
				// // add to menu
				// menu.addMenuItem(deleteItem);
			}
		};
		// set creator
		mMyListView.setMenuCreator(creator);

		// step 2. listener item click event
		mMyListView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public void onMenuItemClick(int position, SwipeMenu menu, int index) {
				// ApplicationInfo item = mAppList.get(position);
				switch (index) {
				case 0:
					
					break;

				}
			}
		});

		// set SwipeListener
		mMyListView.setOnSwipeListener(new OnSwipeListener() {

			@Override
			public void onSwipeStart(int position) {
				// swipe start
			}

			@Override
			public void onSwipeEnd(int position) {
				// swipe end
			}
		});
	}

	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				getResources().getDisplayMetrics());
	}
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) {
		case RESULT_CODE:
			if (data != null) {			
				String name = data.getStringExtra("name");
				String tel = data.getStringExtra("tel");	
				String setout_longitude = data.getStringExtra("setout_longitude");
				String setouts_latitude = data.getStringExtra("setouts_latitude");
				String setout_address = data.getStringExtra("setout_address");
				String destination_longitude = data.getStringExtra("destination_longitude");
				String destination_latitude = data.getStringExtra("destination_latitude");
				String destination_address = data.getStringExtra("destination_address");
				
				if (!name.equals("")&& !tel.equals("")&&!setout_longitude.equals("")&&!setouts_latitude.equals("")&&!setout_address.equals("") 
						&&!destination_longitude.equals("")&&!destination_latitude.equals("")&&!destination_address.equals("")) {
					Map<String, String> map=new HashMap<String, String>();
					map.put("setout",setout_address);
					map.put("destination",destination_address);
					map.put("tel",tel);
					list.add(map);
					mDifferentAddressAdapter.notifyDataSetChanged();
				}
			}
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void initView() {
		list=new ArrayList<Map<String, String>>();
		Map<String, String> map=new HashMap<String, String>();
		map.put("setout",setout_string);
		map.put("destination",destination_string);
		map.put("tel",telephone_string);
		list.add(map);
		
		mDifferentAddressAdapter=new DifferentAddressAdapter(this,list);
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.list_footer, null);
		mMyListView.addFooterView(view,null, false);
		mMyListView.setAdapter(mDifferentAddressAdapter);
		add=(RelativeLayout)view.findViewById(R.id.add);
		add.setOnClickListener(this);
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void desotryItems() {
		// TODO Auto-generated method stub
		
	}
}
